
import puppeteer from 'puppeteer';
import { CPU_NUM } from '../config/env';


const DEVICE_ENUM = {
    pc: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36',
    phone: 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1'
};

// enum DEVICE_ENUM {
//     pc = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36',
//     phone = 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1'
// };

class PuppeteerService {
    private WSE_LIST: string[] = [];

    constructor() {
        this.init();
    }

    private async init() {
        for (let i=0; i < CPU_NUM; i++){
            const browser = await puppeteer.launch({
                headless: 'new',
                args: [
                    // '--disable-gpu',
                    // '--disable-dev-shm-usage',
                    // '--disable-setuid-sandbox',
                    // '--no-first-run',
                    // '--no-sandbox',
                    // '--no-zygote',
                    // '--single-process'
                ]
            });
            const browserWSEndpoint = await browser.wsEndpoint();
            this.WSE_LIST[i] = browserWSEndpoint;
        }
    }

    async getBrowser() {
        const browserWSEndpoint = this.WSE_LIST[Math.floor(Math.random() * CPU_NUM)];
        return puppeteer.connect({browserWSEndpoint});
    };

    async getPageContent(url: string) {
        const browser = await this.getBrowser();
        const page = await browser.newPage();
        // eslint-disable-next-line max-len
        page.setUserAgent('Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1');
        await page.setViewport({width: 375, height: 667, deviceScaleFactor: 1});
        await page.goto(url);
        const content = await page.content();
        await page.close();
        return content;
    }
    
    async getPageCapture(url: string, options = {}) {
        const {device = 'pc', pixel: deviceScaleFactor = 1, captureSize = 'firstScreen'} = options as any;
        const browser = await this.getBrowser();
        const page = await browser.newPage();

        if (device === 'pc') {
            page.setUserAgent(DEVICE_ENUM.pc);
            await page.setViewport({width: 1242, height: 667, deviceScaleFactor, isMobile: false});
        } else {
            page.setUserAgent(DEVICE_ENUM.phone);
            await page.setViewport({width: 375, height: 667, deviceScaleFactor, isMobile: true});
        }

        if (captureSize === 'fullScreen') {
        }

        await page.goto(url);
        await  page.content();
        return page.screenshot();
    }
}

export default new PuppeteerService();
